package com.nlp.shop.web.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Date;

import com.jfinal.core.Controller;
/**
 * 
 * @author zyl
 *
 */
public class BaseController extends Controller {

	protected String readContent() throws IOException {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		InputStream inputStream = this.getRequest().getInputStream();

		byte[] buf = new byte[4096];
		int readLen = 0;
		while ((readLen = inputStream.read(buf, 0, 4096)) != -1) {
			outputStream.write(buf, 0, readLen);
		}

		return new String(outputStream.toByteArray());
	}

	@Override
	public String getPara(String name) {
		String result = super.getPara(name);
		if (result == null && getAttr(name) != null) {
			result = String.valueOf((Object) getAttr(name));
		}
		return result;
	}

	@Override
	public String getPara(String name, String defaultValue) {
		String result = super.getPara(name);
		if (result == null && getAttr(name) != null) {
			result = String.valueOf((Object) getAttr(name));
		}
		return result != null && !"".equals(result) ? result : defaultValue;
	}

	@Override
	public Long getParaToLong(String name) {
		Long result = super.getParaToLong(name);
		if (result == null && getAttr(name) != null) {
			result = toLong(String.valueOf((Object) getAttr(name)), null);
		}
		return result;
	}
	
	@Override
	public Long getParaToLong(String name, Long defaultValue) {
		Long result = super.getParaToLong(name);
		if (result == null && getAttr(name) != null) {
			result = toLong(String.valueOf((Object) getAttr(name)), null);
		}
		return result != null ? result : defaultValue;
	}
	
	@Override
	public Integer getParaToInt(String name) {
		Integer result = super.getParaToInt(name);
		if (result == null && getAttr(name) != null) {
			result = toInt(String.valueOf((Object) getAttr(name)), null);
		}
		return result;
	}
	
	@Override
	public Integer getParaToInt(String name, Integer defaultValue) {
		Integer result = super.getParaToInt(name);
		if (result == null && getAttr(name) != null) {
			result = toInt(String.valueOf((Object) getAttr(name)), null);
		}
		return result != null ? result : defaultValue;
	}
	
	@Override
	public Date getParaToDate(String name) {
		Date result = super.getParaToDate(name);
		if (result == null && getAttr(name) != null) {
			result = toDate(String.valueOf((Object) getAttr(name)), null);
		}
		return result;
	}
	
	private Integer toInt(String value, Integer defaultValue) {
		if (value == null || "".equals(value.trim()))
			return defaultValue;
		if (value.startsWith("N") || value.startsWith("n"))
			return -Integer.parseInt(value.substring(1));
		return Integer.parseInt(value);
	}
	
	private Long toLong(String value, Long defaultValue) {
		if (value == null || "".equals(value.trim()))
			return defaultValue;
		if (value.startsWith("N") || value.startsWith("n"))
			return -Long.parseLong(value.substring(1));
		return Long.parseLong(value);
	}
	
	private Date toDate(String value, Date defaultValue) {
		if (value == null || "".equals(value.trim()))
			return defaultValue;
		try {
			return new java.text.SimpleDateFormat("yyyy-MM-dd").parse(value);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}
}

